package cn.itwangxiang.android.ui;



import cn.itwangxiang.android.AppContext;
import cn.itwangxiang.android.R;
import android.annotation.TargetApi;
import android.app.Activity;
import android.content.ClipboardManager;
import android.content.Context;
import android.content.Intent;
import android.os.Build;
import android.os.Bundle;
import android.view.View;
import android.widget.Button;
import android.widget.EditText;
import android.widget.Toast;

public class DemoIntent extends Activity {

	private Button button1;
	private Button button2;
	private Button button3;
	private Button button4;
	private EditText editText1;
	
	public ClipboardManager cm ;

	@TargetApi(Build.VERSION_CODES.HONEYCOMB)
	@Override
	protected void onCreate(Bundle savedInstanceState) {
		super.onCreate(savedInstanceState);
		setContentView(R.layout.demo_intent);
		
		//剪切板
		cm = (ClipboardManager) getSystemService(Context.CLIPBOARD_SERVICE);

		button1 = (Button) findViewById(R.id.button1);
		button2 = (Button) findViewById(R.id.button2);
		button3 = (Button) findViewById(R.id.button3);
		button4 = (Button) findViewById(R.id.button4);
		editText1 = (EditText) findViewById(R.id.editText1);
		
		String clipboardMsg = "";
		try {
			clipboardMsg = cm.getPrimaryClip().getItemAt(0).getText().toString();
		} catch (Exception e) {
		}
		
		editText1.setText(clipboardMsg);
		
		
		button1.setOnClickListener(new View.OnClickListener() {

			@Override
			public void onClick(View v) {

				String name = getIntent().getStringExtra("name");

				int age = getIntent().getIntExtra("age", 0);

				Toast.makeText(DemoIntent.this,
						"获取上个Intent中传递的用户信息：name=" + name + ",age=" + age,
						Toast.LENGTH_LONG).show();
			}
		});

		button2.setOnClickListener(new View.OnClickListener() {

			@Override
			public void onClick(View v) {
				String name = getIntent().getStringExtra("name");
				int age = getIntent().getIntExtra("age", 0);
				Intent intent = new Intent(DemoIntent.this,Demo.class);
				intent.putExtra("user", "name:"+name+",age:"+age+",email:913083524@qq.com");
				setResult(2, intent);
				finish();//结束生命周期。
			}
		});

		button3.setOnClickListener(new View.OnClickListener() {

			@Override
			public void onClick(View v) {
				
				/*//将对象数据转换String传递
				User user = new User("kojln", "24");
				ByteArrayOutputStream bao = new ByteArrayOutputStream();
				String base64String = "";
				try {
					ObjectOutputStream oos = new ObjectOutputStream(bao);
					oos.writeObject(user);
					base64String = Base64.encodeToString(bao.toByteArray(),
							Base64.DEFAULT);
					oos.close();
				} catch (Exception e) {
				}*/
				
			/*	// 将String恢复成对象数据
				byte[] bye = Base64.decode(base64String, Base64.DEFAULT);
				ByteArrayInputStream bos = new ByteArrayInputStream(bye);
				try {
					ObjectInputStream oos = new ObjectInputStream(bos);
					User user = (User) oos.readObject();
				} catch (Exception e) {
				}*/
				
				String newClipboard = editText1.getText().toString();
				cm.setText(newClipboard);
				Intent intent = new Intent(DemoIntent.this,Main.class);
				setResult(3, intent);
				finish();
			}
		});

		button4.setOnClickListener(new View.OnClickListener() {

			@Override
			public void onClick(View v) {

				Toast.makeText(
						DemoIntent.this,
						"从全局变量中获取当前网络是否可用："
								+ ((AppContext) getApplication())
										.isNetworkConnected(),
						Toast.LENGTH_LONG).show();
			}
		});

	}

}
